package cn.yunyichina.provider.bone.service.impl;

import cn.yunyichina.provider.bone.dao.ExtensionDao;
import cn.yunyichina.provider.bone.entity.Extension;
import cn.yunyichina.provider.bone.vo.ExtensionVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import cn.yunyichina.utils.convert.PinyinUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuxp on 2017/12/11.
 *
 * @param
 * @Descrition
 */
@Service
public class ExtensionServiceImpl {

    private SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static Logger logger = LoggerFactory.getLogger(ExtensionServiceImpl.class);

    @Autowired
    private ExtensionDao extensionDao;

    public Response listExtension(String data){
        Response response = new Response();
        try {
            ExtensionVo extensionVo = JsonUtils.parseObject(data,ExtensionVo.class);
            List<ExtensionVo> extensionVos = extensionDao.queryExtensionList(extensionVo);
            if(extensionVos.size()>0){
                List<String> firstWords = new ArrayList<>();
                Map<String,Object> map = new HashMap<>();
                for(ExtensionVo entity:extensionVos){
                    if(entity.getUserName()!=null){
                        String fristChinesWord = PinyinUtils.getChineseFirstWord(entity.getUserName()).substring(0,1);
                        firstWords.add(fristChinesWord);
                    }
                }
                for (String firstWord:firstWords){
                    List<ExtensionVo> list = new ArrayList<>();
                    for(ExtensionVo entity:extensionVos){
                        if(entity.getUserName()!=null){
                            String thisWord = PinyinUtils.getChineseFirstWord(entity.getUserName()).substring(0,1);
                            if(firstWord.equals(thisWord)){
                                list.add(entity);
                            }
                        }
                    }
                    map.put(firstWord,list);
                }
                response.setResultCodeAndMessageAndResult("0","查询患者列表成功!",JsonUtils.toJsonString(map));
            }else {
                response.setResultCodeAndMessageAndResult("0","查询患者列表成功！",JsonUtils.toJsonString(extensionVos));
            }
        }catch (Exception e){
            logger.error("查询患者列表异常",e);
            response.setResultCodeAndMessage("-1","查询患者列表异常!");
        }
        return response;
    }
}
